package com.lovo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.model.DOS.UserDO;
import com.lovo.model.DTO.UserDTO;
import com.lovo.result.ResponseResult;
import com.lovo.service.IUserService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * Description:
 *
 * @author LiuChengQi
 * @date Created on 2020/11/25
 */
@RestController
@Api("登录注册控制器")
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService service;

    @ResponseResult
    @GetMapping("/login")
    @ApiOperation("登录用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginName", value = "用户名" ,example = "q"),
            @ApiImplicitParam(name = "pwd", value = "密码" ,example = "1")
    })
    public Object login(String loginName, String pwd){

        UserDTO userDTO = new UserDTO();
        QueryWrapper<UserDO> userDOQueryWrapper = new QueryWrapper<>();
        userDOQueryWrapper.eq("u_loginName",loginName).eq("u_pwd",pwd);
        List<UserDO> list = service.list(userDOQueryWrapper);
        BeanUtils.copyProperties( list.get(0),userDTO);

        return userDTO;
    }
    @ResponseResult
    @PostMapping("/register")
    @ApiOperation("注册用户")
    public Object register(@RequestBody @Valid UserDTO userDTO){
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userDTO,userDO);
        boolean save = service.save(userDO);
        if (save){
            return 1;

        }
        return 0;

    }




























}
